package com.fount4j.generator.jdbc;

import com.fount4j.generator.exception.ConnectException;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * 从数据源获取数据库连接
 *
 * @author Morven 2016/12/20
 */
public class DataSourceConnectionFactory implements ConnectionFactory {
    /** 数据源 */
    private DataSource dataSource;

    public DataSourceConnectionFactory(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            throw new ConnectException("从数据源获取连接出错", e);
        }
    }
}
